<?php
require_once('config.inc.php');//$pdo; $uid; ROOT
//print_r($_POST);
//file_put_contents('update.txt', $_POST);
//sleep(10);

$result_ins_rows = 0;
$result_rows = 0;
// 非法操作
if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['password1']) || empty($_POST['email']) || empty($_POST['code'])){
	$result['success'] = 0;
	$result['msg'] = '非法操作!上面表单请认真填写！';
	die(json_encode($result));
}
if(strtolower($_POST['code']) != strtolower($_SESSION['verify'])){
	$result['success'] = 0;
	$result['msg'] = '验证码不正确!';
	die(json_encode($result));
}

$uname = trim($_POST['username']);
$pwd = trim($_POST['password']);
$email = trim($_POST['email']);




$sql = "SELECT uname,email FROM kp_usr WHERE uname=? OR email=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$uname,$email]);
//$stmt_rows = $stmt->rowCount();//获取查询结果行数
//拿到查询结果(二维数组)
$result_all = $stmt->fetchAll(PDO::FETCH_ASSOC);
//拿到结果的行数
$result_rows = count($result_all);
//print_r($result_all);
/*while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    print_r($row);
}*/

//用户名与邮件还没有被注册,写入数据库,返回成功信息
if($result_rows < 1){
	$sql = 'INSERT INTO kp_usr(uname,upwd,email) VALUE(?,?,?)';
	$stmt = $pdo->prepare($sql);
	$result_ins_rows = $stmt->execute([$uname,$pwd,$email]);//写入数据库
	if($result_ins_rows == 1){
		$result['success'] = 1;
		$result['msg'] = '注册成功!';
	}else{
		$result['success'] = 0;
		$result['msg'] = '写入数据库失败!';
	}

}else{
	$result['success'] = 0;
	$result['msg'] = '注册失败,用户名或邮箱地址已被注册,请修改!';
}
die(json_encode($result));






?>